Some Examples of Generics in Java 1.5
نویسنده
چکیده
The forthcoming releases of Java JDK v1.5 and C# v2.0 support generic classes (classes with generic type parameters) and generic methods. Furthermore, each also supports constrained generic types. Much has been written about generic types. In " A Comparative Study of Language Support for Generic Programming " by Garcia et al details of generic types and programming in C++, Haskell, Standard ML, Eiffel, Java and C# are compared. Useful details and generic coding examples from the soon to be released Java JDK 1.5 (now in beta) are presented in the paper " Generics in the Java Programming Language " by Gilad Bracha. Some of the examples used in this column were inspired by this paper. Generic types in Java and C# introduce more expressiveness at the source code level and move type checking from run-time to compile-time when inserting objects into generic collections. In the current pre-generic versions of Java and C#, genericity in collections is obtained through the backdoor of using the universal super type Object as a polymorphic placeholder for the actual reference type that defines the objects to be inserted into the collection. Other than programmer comments, there is little in the source code that reveals the type of object that the programmer intends to hold in the collection. Only by carefully reverse-engineering the source code does this become apparent if in fact only a single type is inserted into the non-generic collection. Generic collections in both Java and C# provide useful self-documentation in the source code in addition to strengthening compile-time type checking. Java and C# use invariant generic typing in contrast with Eiffel which uses covariant typing. In Java and C# a List is not a subtype of a List. These are considered separate stand-alone classes. In Eiffel, a List would be considered a subtype of a List if B conforms to A (B is a descendent of A or equal to A). Although covariant typing generally leads to more flexibility in the use of generics, it has been shown to allow code that is statically correct but fails at runtime (see " Type-Save
منابع مشابه
MultiJava, JML, and Generics
The goal of this semester project is to extend the already existing support for generics in the MultiJava compiler by implementing special features of Java generics on top of it, namely wildcards and raw types, while also making the necessary modifications to JML. To that end, we will give a brief overview of Java generics and describe the main design decisions behind its specification before p...
متن کاملThe Cost of Erasure in Java Generics Type System
Java generics, found in versions 1.5 and later, are implemented by generating only one byte code representation of a generic type or method; a compiler maps all the instantiations of a generic type or method to that unique representation via type erasure. The essence of type erasure is the removal during compilation of all information that is related to type parameters and type arguments. From ...
متن کاملThe Java 5 generics compromise orthogonality to keep compatibility
In response to a long-lasting anticipation by the Java community, version 1.5 of the Java 2 platform referred to as Java 5 introduced generic types and methods to the Java language. The Java 5 generics are a significant enhancement to the language expressivity because they allow straightforward composition of new generic classes from existing ones while reducing the need for a plethora of type ...
متن کاملBeyond Generics: Meta-Level Parameterization for Effective Generic Programming
Generic (polymorphic) type-safe containers are the primary motivation for generics (in Ada, Eiffel, and recently proposed additions to Java and C#) and templates (in C++). We studied buffer classes and found that they could not be unified by type parameters. At times, unification required non-type parameters (e.g., parameters representing operators, keywords or algorithmic details) and at other...
متن کاملOwnership Domains in the Real World
The Ownership Domains type system has had publicly available tool support for a few years. However, the previous implementation used non-backwards compatible language extensions to Java and ran on a research infrastructure, which made it difficult to conduct substantial case studies on interesting systems. We first present a re-implementation of ownership domains using Java 1.5 annotations and ...
متن کاملذخیره در منابع من
با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید
عنوان ژورنال:
- Journal of Object Technology
دوره 3 شماره
صفحات -
تاریخ انتشار 2004